﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace ModernUILogViewer.Business
{
    static class Utility
    {
        public static bool IsValidRegex(string pattern)
        {
            try
            {
                Regex.Match(String.Empty, pattern);
            }
            catch (ArgumentException)
            {
                return false;
            }

            return true;
        }

        public static Version GetProductVersion()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);

            return new Version(fileVersionInfo.ProductVersion);
        }

        public static string GetAppLocation()
        {            
            return System.Reflection.Assembly.GetExecutingAssembly().Location;
        }

        public static string GetPropertyName<P, T>(Expression<Func<P, T>> expression)
        {
            if (expression.Body is UnaryExpression)
            {
                UnaryExpression unex = (UnaryExpression)expression.Body;
                if (unex.NodeType == ExpressionType.Convert)
                {
                    Expression ex = unex.Operand;
                    MemberExpression mex = (MemberExpression)ex;
                    return mex.Member.Name;
                }
            }

            MemberExpression memberExpression = (MemberExpression)expression.Body;
            return memberExpression.Member.Name;
        }

        public static object GetPropValue(object src, string propName)
        {
            return src.GetType().GetProperty(propName).GetValue(src, null);
        }

        public static bool IsUrl(string path)
        {
            return Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute);
        }


    }
}
